http = require "socket.http"
json = (loadfile "JSON.lua" )()

local citycodefile = io.open("citycode.json", "r")
local citycodejson = citycodefile:read('a')
citycodefile:close()
local citycodetab = json:decode(citycodejson)


function GetCityCode(city,id)
    for _, tab in pairs(citycodetab) do
        if tab.city_name:find(city) or tab.id == id then return tab.city_code end
        --else pass
    end
    return nil
end

function GetWeather (city)
    local translate = {
        notice  = "当日提醒： ",
        type    = "天气： ",
        date    = "日期： ",
        fl      = "风力： ",
        aqi     = "AQI指数： ",
        fx      = "风向： ",
        high    = "",
        sunset  = "日落时刻： ",
        sunrise = "日出时刻： ",
        ymd     = "年-月-日： ",
        week    = "",
        low     = ""
    }
    
    local url = [[http://t.weather.itboy.net/api/weather/city/]]
    --default
    local citycode =  "101030100"
    --city = io.read()
    citycode = GetCityCode(city)
    local response = http.request(url .. citycode)
    local weather = json:decode(response)
    print("【一周天气】")
    for _, tab in pairs(weather.data.forecast) do
        for ind,val in pairs(tab) do 
            print (translate[ind] .. val)
        end
    end

    return city .. [[今日天气：]] ..
    [[  温度：]] .. weather.data.wendu .. [[℃]] ..
    [[  湿度：]] .. weather.data.shidu ..
    [[  PM2.5：]] .. weather.data.pm25
end
--"西安")